package com.yupi.tangoj.model.vo;

import cn.hutool.core.bean.BeanUtil;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.yupi.tangoj.model.dto.question.JudgeConfig;
import com.yupi.tangoj.model.entity.Question;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 题目表
 *
 * @TableName question
 */
@Data
public class QuestionVO implements Serializable {
    /**
     * id
     */
    private Long id;

    /**
     * 题目标题
     */
    private String title;

    /**
     * 题目内容
     */
    private String content;

    /**
     * 题目列表（json数组字符串）
     */
    private List<String> tags;


    /**
     * 提交数
     */
    private Long commAmount;

    /**
     * 通过数
     */
    private Long acAmount;

    /**
     * 点赞数
     */
    private Integer thumbNum;

    /**
     * 收藏数
     */
    private Integer favourNum;

    /**
     * 判题配置
     */
    private JudgeConfig judgeConfig;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 创建人信息
     */
    private UserVO userVO;
    /**
     * 是否删除
     */
    @TableLogic
    private Integer isDelete;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    /**
     * 对象转包装类
     *
     * @param question
     * @return
     */
    public static QuestionVO objToVo(Question question) {

        QuestionVO questionVO = new QuestionVO();
        BeanUtil.copyProperties(question, questionVO);

        String tags = question.getTags();

        if (tags != null) {
            List<String> tagList = JSONUtil.toList(tags, String.class);
            questionVO.setTags(tagList);
        }

        JSONObject judgeConfig = question.getJudgeConfig();


        if (judgeConfig != null) {
            questionVO.setJudgeConfig(JSONUtil.toBean(judgeConfig,JudgeConfig.class));
        }


        return questionVO;
    }

    public static Question voToObj(QuestionVO questionVO) {
        Question question = new Question();

        BeanUtil.copyProperties(questionVO,question);

        List<String> tageList = questionVO.getTags();

        if (tageList != null){
            String tags = JSONUtil.toJsonStr(tageList);
            question.setTags(tags);
        }

        JudgeConfig judgeConfig = questionVO.getJudgeConfig();

        if (judgeConfig != null){
            question.setJudgeConfig(new JSONObject(JSONUtil.toJsonStr(judgeConfig)));
        }
        return question;
    }
}